<html>
<head>
	<meta charset="utf-8"/>
	<title>文件上传</title>
	<script type="text/javascript">
			function readFile() {
				var fileObj = document.getElementById("uploadFile");
				var file = fileObj.files[0];//相当于Java中File
				document.getElementById("fileName").value= file.name;
				var reader = new FileReader();
				reader.readAsDataURL(file);
				//onload方法是读取文件完毕时触发的事件
				//onload=function(){} 将function的实现于onload关联绑定
				reader.onload = function() { 
					//console.log("onload result = " + this.result);
					document.getElementById("fileContent").value=this.result;
					document.getElementById("testImg").src=this.result;
				}
			}
	</script>
</head>
<body>
	<div align="center">
	<form action="base64File.asp" method="Post">
		姓名:<input type="text" name="userName"/>
		<br/>
		照片:<input type="file" id="uploadFile" name="uploadFile" onchange="readFile()"/>
		<input type="hidden" name="fileName" id="fileName"/>
		<input type="hidden" name="fileContent" id="fileContent"/>
		<br/>
		<!-- <input type="button" value="测试读取文件" onClick="getFile()"/>  -->
		<input type="submit" value="上传文件"/>
	</form>
	<img id="testImg" src=""/>
	</div>
</body>
</html>